package com.doctorliang.cheng.shopping.type.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.base.BaseFragment;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

/**
 * Created by CHENG on 2017/9/26.
 * 分类视图的Fragment
 */

public class TypeFragment extends BaseFragment {
    @Bind(R.id.tl_l)
    SegmentTabLayout segmentTabLayout;
    @Bind(R.id.iv_type_search)
    ImageView ivTypeSearch;
    @Bind(R.id.fl_type)
    FrameLayout flType;
    //fragment集合
    private List<BaseFragment> fragmentList;
    //上一个fragment
    private Fragment tempFragment;
    //分类fragment
    private ListFragment listFragment;

    //标签fragment
    private TagFragment tagFragment;
    private FragmentTransaction ft;

    @Override
    public View initView() {
        View view = View.inflate(mcontext, R.layout.fragment_type, null);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        //初始化2个fragment并把它们放到listFragment
        initFragment();
        Log.e(TAG, "分类视图的数据被初始化了");
        String[] titles = {"分类","标签"};
        //设置标题
        segmentTabLayout.setTabData(titles);
        //设置点击事件
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                //切换fragment
                switchFragment(tempFragment,fragmentList.get(position));
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }



    @Override
    public void onResume() {
        super.onResume();
        switchFragment(tempFragment, fragmentList.get(0));
    }


    private void initFragment() {
        fragmentList = new ArrayList<>();
        listFragment = new ListFragment();
        tagFragment = new TagFragment();
        fragmentList.add(listFragment);
        fragmentList.add(tagFragment);

        //默认第0个
        switchFragment(tempFragment,fragmentList.get(0));
    }

    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if (tempFragment!=nextFragment) {
            tempFragment = nextFragment;//保存下一个
            if (nextFragment!=null) {//下一个不等于空才切换
                //打开事务
                ft = getActivity().getSupportFragmentManager().beginTransaction();
                //判断下一个是否被添加了
                if (!nextFragment.isAdded()) {//没有添加
                    //隐藏当前的
                    if (fromFragment != null) {
                        ft.hide(fromFragment);
                    }
                    ft.add(R.id.fl_type,nextFragment,"tagFragment").commit();//千万不要忘记commit了
                } else {//已添加
                    //hide当前，show下一个
                    if (fromFragment!=null) {
                        ft.hide(fromFragment);
                    }
                    ft.show(nextFragment).commit();
                }
//                ft.commit();
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
